home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * swcurve
- * sweep a curve to make a solid
- *
- * Paul Haeberli - 1990
- */
- #include "math.h"
- #include "stdio.h"
- #include "sgiobj.h"
- #include "vect.h"
-
- #define MAXPOINTS 1000
- #define MAXRUNS 100
-
- int endpos[MAXPOINTS];
- float points[MAXPOINTS][3];
- int npoints;
- int flat = 0;
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- int nsteps, npolys, nedges;
- float angle;
- vect n0, n1;
- int p0;
- sgiobj *obj;
- float *dptr;
-
- if(argc<5) {
- fprintf(stderr,"usage: swcurve in.cur out.bin nsteps angle [-f]\n");
- exit(1);
- }
- if(argc>5)
- flat = 1;
- readpoints(argv[1]);
- findruns();
- nedges = npoints-1;
-
- nsteps = atoi(argv[3]);
- angle = atof(argv[4]);
- npolys = nsteps*nedges;
-
- obj = newquadobj(npolys);
- dptr = (float *)obj->data;
- for(p0=0; p0<nedges; p0++) {
- if(endpos[p0])
- setnormal2(&n0,p0,p0+1);
- else
- setnormal3(&n0,p0-1,p0,p0+1);
- if(endpos[p0+1])
- setnormal2(&n1,p0,p0+1);
- else
- setnormal3(&n1,p0,p0+1,p0+2);
- dosweep(&n0,points+p0,&n1,points+p0+1,nsteps,angle,dptr);
- dptr += 4*nsteps*PNTLONGS;
- }
- writesgiobj(argv[2],obj);
- }
-
- dosweep(n0,p0,n1,p1,nsteps,angle,data)
- vect *n0,*p0,*n1,*p1;
- int nsteps;
- float angle;
- float *data;
- {
- int i;
- float a, s, c;
- vect rn0, rp0, rn1, rp1;
-
- for(i=0; i<nsteps; i++) {
- a = ((i+0.0)*angle)/nsteps;
- s = sin(a*M_PI/180.0);
- c = cos(a*M_PI/180.0);
- vrot(p0,&rp0,s,c);
- vrot(p1,&rp1,s,c);
- if(flat) {
- a = ((i+0.5)*angle)/nsteps;
- s = sin(a*M_PI/180.0);
- c = cos(a*M_PI/180.0);
- }
- vrot(n0,&rn0,s,c);
- vrot(n1,&rn1,s,c);
- copytodata(data,&rn0,&rp0,&rn1,&rp1);
- a = ((i+1.0)*angle)/nsteps;
- s = sin(a*M_PI/180.0);
- c = cos(a*M_PI/180.0);
- data += 2*PNTLONGS;
- vrot(p0,&rp0,s,c);
- vrot(p1,&rp1,s,c);
- if(flat) {
- a = ((i+0.5)*angle)/nsteps;
- s = sin(a*M_PI/180.0);
- c = cos(a*M_PI/180.0);
- }
- vrot(n0,&rn0,s,c);
- vrot(n1,&rn1,s,c);
- copytodata(data,&rn1,&rp1,&rn0,&rp0);
- data += 2*PNTLONGS;
- }
- }
-
- vrot(p0,p1,s,c)
- vect *p0, *p1;
- float s, c;
- {
- p1->x = s*p0->x + c*p0->z;
- p1->y = p0->y;
- p1->z = s*p0->z - c*p0->x;
- }
-
- copytodata(data,n0,p0,n1,p1)
- float *data;
- float *n0,*p0,*n1,*p1;
- {
- data[OFFSET_NORMAL+0] = n0[0];
- data[OFFSET_NORMAL+1] = n0[1];
- data[OFFSET_NORMAL+2] = n0[2];
- data[OFFSET_POINT+0] = p0[0];
- data[OFFSET_POINT+1] = p0[1];
- data[OFFSET_POINT+2] = p0[2];
- data += PNTLONGS;
- data[OFFSET_NORMAL+0] = n1[0];
- data[OFFSET_NORMAL+1] = n1[1];
- data[OFFSET_NORMAL+2] = n1[2];
- data[OFFSET_POINT+0] = p1[0];
- data[OFFSET_POINT+1] = p1[1];
- data[OFFSET_POINT+2] = p1[2];
- }
-
- setnormal2(n,p0,p1)
- vect *n;
- {
- n->x = (points[p1][1]-points[p0][1]);
- n->y = -(points[p1][0]-points[p0][0]);
- n->z = 0.0;
- vnormal(n);
- }
-
- setnormal3(n,p0,p1,p2)
- vect *n;
- {
- vect v0, v1, c;
-
- v0.x = points[p0][0]-points[p1][0];
- v0.y = points[p0][1]-points[p1][1];
- v0.z = 0.0;
- vnormal(&v0);
- v1.x = points[p2][0]-points[p1][0];
- v1.y = points[p2][1]-points[p1][1];
- v1.z = 0.0;
- vnormal(&v1);
- if(myvhalf(&v0,&v1,n)) {
- vcross(n,&v0,&c);
- if(c.z>0.0) {
- n->x = -n->x;
- n->y = -n->y;
- n->z = -n->z;
- }
- } else {
- setnormal2(n,p1,p2);
- }
- }
-
- myvhalf(v1,v2,half)
- vect *v1,*v2,*half;
- {
- float len;
-
- vadd(v2,v1,half);
- len = vlength(half);
- if(len>0.001) {
- vscale(half,1.0/len);
- return 1;
- } else
- return 0;
- }
-
- findruns()
- {
- int i, dest;
- float x, y;
-
- for(i=0; i<npoints; i++)
- endpos[i] = 0;
- dest = 0;
- x = points[0][0];
- y = points[0][1];
- endpos[dest] = 1;
- dest++;
- for(i=1; i<npoints; i++) {
- if(points[i][0] != x || points[i][1] != y) {
- x = points[dest][0] = points[i][0];
- y = points[dest][1] = points[i][1];
- endpos[dest] = 0;
- dest++;
- } else {
- endpos[dest-1] = 1;
- }
- }
- endpos[dest-1] = 1;
- npoints = dest;
- }
-
- readpoints(name)
- char *name;
- {
- FILE *inf;
- int n, r;
- float x, y;
- char oneline[256];
-
- inf = fopen(name,"r");
- if(!inf) {
- fprintf(stderr,"can't open input file %s\n",name);
- exit(1);
- }
- n = 0;
- while(fgets(oneline,255,inf)) {
- sscanf(oneline,"%f %f",&x,&y);
- points[n][0] = x;
- points[n][1] = y;
- n++;
- }
- npoints = n;
- fclose(inf);
- }
-